/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.stockkeeper.app.module;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.letmeshare.stockkeeper.PersistenceManager;
import org.letmeshare.stockkeeper.org.entities.User;

/**
 *
 * @author gAmE wOrLd
 */
public class AppModule {
    
    public static boolean isValidLogin() {
        EntityManager em = PersistenceManager.getEntityManager();
        
        Query query = em.createQuery("SELECT user FROM User user");
        List<User> users = query.getResultList();
        if (users.isEmpty()) {
            User user = new User();
            user.setCreatedOn(new Date());
            em.persist(user);
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(users.get(0).getCreatedOn());
            cal.add(Calendar.DAY_OF_MONTH, 3);
            if (new Date().compareTo(cal.getTime())>0) {
                return false;
            }
        }
        return true;
    }
}
